//
//  HDCFMakeUpTableViewController.m
//  HaoDeCaiFu
//
//  Created by 张玉鹏 on 2017/11/17.
//  Copyright © 2017年 郑开宏. All rights reserved.
//

#import "HDCFMakeUpTableViewController.h"
#import "makeUpTableViewCell.h"
#import "HDCFBuyMakeUp.h"
#import "SVProgressHUD.h"
#import "HDCFLoginVc.h"
#import "HDCFOpenAccountVc.h"
#import <WebKit/WebKit.h>
#import "HDCFFundDetailVc.h"
#import "HDCFFixThrowViewController.h"//定投vc

@interface HDCFMakeUpTableViewController ()<WKUIDelegate, WKNavigationDelegate>
{
    UIButton *_lastBtn;
    UIButton *_lastFiveBtn;
    UIView   *_fiveBtnBottomLine;
    NSString *_textStr;
}
@property(nonatomic,strong)UIView *firstSectionHeaderView;
@property(nonatomic,strong)UIView *secondSectionHeaderView;
@property(nonatomic,strong)UILabel *thirdSectionHeaderView;
@property(nonatomic,strong)UIView *secondSectionfooterView;
@property(nonatomic,strong)UIView *clearHudView;

@property (nonatomic, weak) WKWebView *webView;

@property (nonatomic, strong) NSMutableArray *oneMounth;
@property (nonatomic, strong) NSMutableArray *threeMounth;
@property (nonatomic, strong) NSMutableArray *sixMounth;
@property (nonatomic, strong) NSMutableArray *oneYear;

@property (nonatomic, strong) NSDictionary *makeUpData;

@property (nonatomic, assign) BOOL isAvalableBuy;

@end

@implementation HDCFMakeUpTableViewController

- (void)setPortfolioCode:(NSString *)portfolioCode{
    _portfolioCode = portfolioCode;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isAvalableBuy = YES;
    self.title = @"组合基金";
    
    [self.tableView registerNib:[UINib nibWithNibName:@"makeUpTableViewCell" bundle:nil] forCellReuseIdentifier:@"makeUpTableViewCell"];
    
    [self configerTableFooterView];
    [self initBottomView];
    [self getMakeUpDetailData];
    [self getTrendDataWithDate:@"1"];
}

- (void)getTrendDataWithDate:(NSString *)date{
    NSMutableDictionary *paramets = [NSMutableDictionary dictionary];
    if (self.portfolioCode&&![self.portfolioCode isEqual:[NSNull null]]) {
        paramets[@"portfolioCode"] =self.portfolioCode;
    }
    paramets[@"period"] = date;
    NSString *jsonStr = [self dictionaryToJson:paramets];
    NSDictionary *paramet = @{@"paramList":jsonStr,
                              @"methodPath":@"/public/product/portfolio/get-product-portfolio-performance"};
    [HttpRequest sendGetRequestWithURL:nil WithParamets:paramet success:^(id result) {
        NSString *code = result[@"code"];
        if ([code isEqualToString:@"0000"]) {
            [SVProgressHUD dismiss];
            NSDictionary *dict = result[@"data"];
            if ([date isEqualToString:@"1"]) {
                NSDictionary *dictTwo = dict[@"month_1"];
                self.oneMounth = [NSMutableArray arrayWithArray:dictTwo[@"performanceChartDetails"]];
            }else if ([date isEqualToString:@"2"]){
                NSDictionary *dictTwo = dict[@"month_3"];
                self.threeMounth = [NSMutableArray arrayWithArray:dictTwo[@"performanceChartDetails"]];
                [self setHtmlDataWith:_threeMounth];
            }else if ([date isEqualToString:@"3"]){
                NSDictionary *dictTwo = dict[@"month_6"];
                self.sixMounth = [NSMutableArray arrayWithArray:dictTwo[@"performanceChartDetails"]];
                [self setHtmlDataWith:_sixMounth];
            }else if ([date isEqualToString:@"4"]){
                NSDictionary *dictTwo = dict[@"year_1"];
                self.oneYear = [NSMutableArray arrayWithArray:dictTwo[@"performanceChartDetails"]];
                [self setHtmlDataWith:_oneYear];
            }
//            [self.tableView reloadData];
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"message"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
        
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}

-(void)getMakeUpDetailData
{
    [SVProgressHUD show];
    NSMutableDictionary *paramets = [NSMutableDictionary dictionary];
    if (self.portfolioCode&&![self.portfolioCode isEqual:[NSNull null]]) {
        paramets[@"portfolioCode"] =self.portfolioCode;
    }
    NSString *jsonStr = [self dictionaryToJson:paramets];
    NSDictionary *paramet = @{@"paramList":jsonStr,
                              @"methodPath":@"/public/product/portfolio/get-product-portfolio-details"};
    
    [HttpRequest sendGetRequestWithURL:nil WithParamets:paramet success:^(id result) {
        NSString *code = result[@"code"];
        if ([code isEqualToString:@"0000"]) {
//            [SVProgressHUD showSuccessWithStatus:result[@"message"]];
            [SVProgressHUD dismiss];
//            NSDictionary*dic=(NSDictionary*)result;
//            NSDictionary*dataDic=[dic objectForKey:@"data"];
            self.makeUpData = result[@"data"];
            NSLog(@"%@",result);
            self.portDetail=[portfolioDetail mj_objectWithKeyValues:result];
            NSLog(@"%@",self.portDetail.data.portfolioFund);
            [self.tableView setTableHeaderView:[self configerTableHeader]];
            [self.tableView reloadData];
            
            
        }else{
            self.isAvalableBuy = NO;
            [SVProgressHUD showInfoWithStatus:result[@"message"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
        
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}
- (NSString *)dictionaryToJson:(NSDictionary *)dic{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}


-(UITableView*)tableView
{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight-49-KNavHeight)];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        [self.view addSubview: _tableView];
    }
    return _tableView;
}


-(void)configerTableFooterView
{
    UIButton *btnTwoYF = [[UIButton alloc] init];
    btnTwoYF.frame = CGRectMake(0, 0, screenWidth, 50);
    [btnTwoYF setBackgroundColor:ZKcolor(245, 245, 245)];
    NSString *strleft = @"基金销售服务由奕丰金融提供\n-基金有风险 投资需谨慎-";
    NSMutableAttributedString *attrStrT = [[NSMutableAttributedString alloc] initWithString:strleft];
    [attrStrT addAttribute:NSForegroundColorAttributeName
                     value:[UIColor lightGrayColor]
                     range:NSMakeRange(0, strleft.length)];
    [attrStrT addAttribute:NSFontAttributeName
                     value:[UIFont boldSystemFontOfSize:12]
                     range:NSMakeRange(0, strleft.length)];
    [attrStrT addAttribute:NSFontAttributeName
                     value:[UIFont boldSystemFontOfSize:14]
                     range:NSMakeRange(7, 4)];
    [attrStrT addAttribute:NSForegroundColorAttributeName
                     value:ZKcolor(33, 33, 33)
                     range:NSMakeRange(7, 4)];
    [btnTwoYF setAttributedTitle:attrStrT forState:UIControlStateNormal];
    [btnTwoYF setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btnTwoYF.titleLabel setTextAlignment:NSTextAlignmentCenter];
    btnTwoYF.titleLabel.lineBreakMode = 0;
    [btnTwoYF addTarget:self action:@selector(presentPage) forControlEvents:UIControlEventTouchUpInside];
    [self.tableView setTableFooterView:btnTwoYF];

}
-(void)initBottomView
{
    UIView * bottomView = [UIView new];
    bottomView.frame = CGRectMake(0, screenHeight - 49-KNavHeight , screenWidth, 49);
    [self.view addSubview:bottomView];
    
    //定投
//    UIButton *fixThrowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    fixThrowBtn.frame = CGRectMake(0, 0 , screenWidth/3.0, 49);
//    [fixThrowBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [fixThrowBtn setTitle:@"定投" forState:UIControlStateNormal];
//    [fixThrowBtn addTarget:self action:@selector(fixThrowClick) forControlEvents:UIControlEventTouchUpInside];
//    [fixThrowBtn setBackgroundColor:ZKcolor(184, 27, 36)];
//    [bottomView addSubview:fixThrowBtn];
//
//    UILabel * lineOne = [UILabel new];
//    lineOne.frame = CGRectMake(screenWidth/3.0-0.5, 0,0.5, 49);
//    lineOne.backgroundColor = [UIColor whiteColor];
//    [bottomView addSubview:lineOne];
    
    //添加购物车
//    UIButton *addShopBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    addShopBtn.frame = CGRectMake(screenWidth/3.0, 0 , screenWidth/3.0, 49);
//    [addShopBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [addShopBtn setTitle:@"添加购物车" forState:UIControlStateNormal];
//    [addShopBtn addTarget:self action:@selector(addShop) forControlEvents:UIControlEventTouchUpInside];
//    [addShopBtn setBackgroundColor:ZKcolor(184, 27, 36)];
//    [bottomView addSubview:addShopBtn];
//
//    UILabel * lineTwo = [UILabel new];
//    lineTwo.frame = CGRectMake(screenWidth/3.0*2-0.5, 0,0.5, 49);
//    lineTwo.backgroundColor = [UIColor whiteColor];
//    [bottomView addSubview:lineTwo];
    
    //立即购买
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0 , screenWidth, 49);
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitle:@"立即购买" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(jumpBuy) forControlEvents:UIControlEventTouchUpInside];
    [btn setBackgroundColor:ZKcolor(184, 27, 36)];
    [bottomView addSubview:btn];
}

- (void)presentPage{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIView *view = [[UIView alloc] init];
    self.clearHudView = view;
    view.frame = CGRectMake(0, 0, screenWidth, screenHeight);
    UITapGestureRecognizer *r5 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewHit)];
    r5.numberOfTapsRequired = 1;
    [view addGestureRecognizer:r5];
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(30, (screenHeight - (screenWidth - 60) * 900.0 / 500.0) / 2, screenWidth - 60, (screenWidth - 60) * 900.0 / 500.0);
    imageView.image = [UIImage imageNamed:@"661511947049_.pic_hd.jpg"];
    [view addSubview:imageView];
    view.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.1];
    [window addSubview:view];
}

- (void)viewHit{
    [self.clearHudView removeFromSuperview];
}
#pragma mark -
#pragma mark 点击事件
////定投
//-(void)fixThrowClick
//{
//    HDCFFixThrowViewController * fixThrowVC = [HDCFFixThrowViewController new];
//    [self.navigationController pushViewController:fixThrowVC animated:YES];
//}
////添加购物车
//-(void)addShop
//{
//    LLLog(@"添加购物车");
//}
-(void)jumpBuy
{
    NSString *token = kToken;
    if (!(token.length > 0)) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先登录！" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            HDCFLoginVc *loginVc = [[HDCFLoginVc alloc] init];
            [self.navigationController pushViewController:loginVc animated:NO];
            
        }];
        UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction: action1];
        [alert addAction: action2];
        [self presentViewController:alert animated:NO completion:nil];
        return;
    }
    
    NSString *openStatus = kOpenStatus;
    if (!(openStatus.integerValue > 0)){
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先开户！" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            HDCFOpenAccountVc *loginVc = [[HDCFOpenAccountVc alloc] init];
            [self.navigationController pushViewController:loginVc animated:NO];
            
        }];
        UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction: action1];
        [alert addAction: action2];
        [self presentViewController:alert animated:NO completion:nil];
        return;
    }
    
    if (self.isAvalableBuy == NO) {
        [SVProgressHUD showInfoWithStatus:@"此组合不存在,不能购买"];
        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"您将使用奕丰金融提供的基金销售服务" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        HDCFBuyMakeUp*vc=[[HDCFBuyMakeUp alloc]init];
        vc.dataDict = self.makeUpData;;
        [self.navigationController pushViewController:vc animated:YES];
    }];
    UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction: action1];
    [alert addAction: action2];
    [self presentViewController:alert animated:NO completion:nil];
    
    
}
-(UIView*)configerTableHeader
{		
    UILabel*headerLable=[[UILabel alloc]initWithFrame:CGRectMake(-1, 0, screenWidth+2, W_RATIO(100))];
    headerLable.text= [NSString stringWithFormat:@"  %@",_makeUpData[@"portfolioName"]];
    headerLable.layer.borderColor=[UIColor lightGrayColor].CGColor;
    headerLable.layer.borderWidth=0.6;
    headerLable.font=[UIFont systemFontOfSize:17];
    return headerLable;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==2) {
        return self.portDetail.data.portfolioFund.count;
    }
    else
    {
        return 1;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        CGSize maxSize=CGSizeMake(screenWidth-20, 200);
        CGSize size=[self sizeWithFont:[UIFont systemFontOfSize:12] maxSize:maxSize string:self.portDetail.data.portfolioStrategy];
        
        return size.height+20;
    }
    else if (indexPath.section==1)
    {
        return 165;
    }
    else
    {
        return 80;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
        return W_RATIO(100);
}


-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return self.firstSectionHeaderView;
    }
    else if (section==1)
    {
        return self.secondSectionHeaderView;
    }
    else
    {
        return self.thirdSectionHeaderView;
    }
}

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section==1) {
        return self.secondSectionfooterView;
    }
    else
    {
        return nil;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section==1) {
        return W_RATIO(100);
    }
    else
    {
        return 0;
    }
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section==2) {
        makeUpTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"makeUpTableViewCell" forIndexPath:indexPath];
        portfolioDetail*detail=self.portDetail.data.portfolioFund[indexPath.row];
        cell.detail=detail;
        return cell;
    }
    
    static NSString *cellIdentifier=@"Cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell)
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
    }
    if (indexPath.section == 1) {
        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
        CGRect rect = CGRectMake(0, 0, screenWidth, 165);
        WKWebView *webview = [[WKWebView alloc] initWithFrame:rect configuration:config];
        webview.navigationDelegate = self;
        webview.scrollView.scrollEnabled = NO;
        webview.UIDelegate = self;
        NSString *path = [[NSBundle mainBundle] pathForResource:@"echarts" ofType:@"html"];
        NSURL *url = [NSURL fileURLWithPath:path];
        [webview loadRequest:[NSURLRequest requestWithURL:url]];
        [cell addSubview:webview];
        _webView = webview;
    }
//    else
//    {
//        for (UIView*view in cell.contentView.subviews) {
//            [view removeFromSuperview];
//        }
//
//    }
    
    if (indexPath.section==0) {
        cell.textLabel.text=self.portDetail.data.portfolioStrategy;
        cell.textLabel.numberOfLines=0;
        cell.textLabel.font=[UIFont systemFontOfSize:12];
    }
    return cell;
}

-(UIView*)firstSectionHeaderView
{
    NSString *agreeStr =[NSString stringWithFormat:@"日涨幅：%.2f%%", [self.portDetail.data.day1Performance floatValue]];
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:agreeStr];
    NSRange range = [agreeStr rangeOfString:[NSString stringWithFormat:@"%.2f%%",[self.portDetail.data.day1Performance floatValue]]];
    [AttributedStr addAttribute:NSFontAttributeName
                          value:[UIFont systemFontOfSize:15]
                          range:range];
    [AttributedStr addAttribute:NSForegroundColorAttributeName
                          value:[UIColor redColor]
                          range:range];
    NSString *shouyiStr =[NSString stringWithFormat:@"近一年收益：%.2f%%",[self.portDetail.data.performance.performanceYear1 floatValue]];
    NSMutableAttributedString *AttributedShouyiStr = [[NSMutableAttributedString alloc]initWithString:shouyiStr];
    NSRange shouyiRange = [shouyiStr rangeOfString:[NSString stringWithFormat:@"%.2f%%",[self.portDetail.data.performance.performanceYear1 floatValue]]];
    [AttributedShouyiStr addAttribute:NSFontAttributeName
                          value:[UIFont systemFontOfSize:15]
                          range:shouyiRange];
    [AttributedShouyiStr addAttribute:NSForegroundColorAttributeName
                          value:[UIColor redColor]
                          range:shouyiRange];
    
    if (!_firstSectionHeaderView) {
        _firstSectionHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth,  W_RATIO(100))];
        _firstSectionHeaderView.backgroundColor = [UIColor whiteColor];
        
        UILabel*leftLable=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, screenWidth/2-10,  W_RATIO(100))];
        leftLable.tag = 111;
        leftLable.attributedText=AttributedStr;
        leftLable.adjustsFontSizeToFitWidth = YES;
        [_firstSectionHeaderView addSubview:leftLable];
        
        UILabel*rightLable=[[UILabel alloc]initWithFrame:CGRectMake(screenWidth/2, 0, screenWidth/2-10,  W_RATIO(100))];
        rightLable.tag=222;
        rightLable.attributedText=AttributedShouyiStr;
        rightLable.textAlignment=2;
        rightLable.adjustsFontSizeToFitWidth = YES;
        [_firstSectionHeaderView addSubview:rightLable];
    }
    else
    {
        UILabel *leftLable=[_firstSectionHeaderView viewWithTag:111];
        leftLable.attributedText=AttributedStr;
        UILabel *rightLable=[_firstSectionHeaderView viewWithTag:222];
        rightLable.attributedText=AttributedShouyiStr;
    }
    return _firstSectionHeaderView;
}

-(UIView*)secondSectionHeaderView
{
    if (!_secondSectionHeaderView) {
        _secondSectionHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, W_RATIO(100))];
        _secondSectionHeaderView.backgroundColor=[UIColor whiteColor];
        NSArray*titleArray=@[@"组合收益走势"];
        for (NSInteger i=0; i<1; i++) {
            UIButton*twoBtn=[UIButton buttonWithType:UIButtonTypeCustom];
            twoBtn.layer.cornerRadius = 5.0;
            twoBtn.clipsToBounds = YES;
            twoBtn.frame=CGRectMake((screenWidth - W_RATIO(280)) / 2, W_RATIO(20), W_RATIO(280), W_RATIO(60));
            [twoBtn setTitle:titleArray[i] forState:UIControlStateNormal];
            twoBtn.titleLabel.font=[UIFont systemFontOfSize:13];
            twoBtn.layer.borderColor=COLOR_NAV.CGColor;
            twoBtn.layer.borderWidth=0.6;
            if (i==0) {
                twoBtn.backgroundColor=COLOR_NAV;
                [twoBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                _lastBtn=twoBtn;
            }
            else
            {
                twoBtn.backgroundColor=[UIColor whiteColor];
                [twoBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            }
            twoBtn.tag=i+100;
            [twoBtn addTarget:self action:@selector(twoBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [_secondSectionHeaderView addSubview:twoBtn];
        
            
        }
    }
    return _secondSectionHeaderView;
}

-(UILabel*)thirdSectionHeaderView
{
    if (!_thirdSectionHeaderView) {
        _thirdSectionHeaderView=[[UILabel alloc]initWithFrame:CGRectMake(-1, 0, screenWidth+2, W_RATIO(100))];
        _thirdSectionHeaderView.backgroundColor=[UIColor whiteColor];
        _thirdSectionHeaderView.textColor=[UIColor blackColor];
        _thirdSectionHeaderView.text=@"组合基金";
        _thirdSectionHeaderView.layer.borderColor=[UIColor lightGrayColor].CGColor;
        _thirdSectionHeaderView.layer.borderWidth=0.6;
        _thirdSectionHeaderView.font=[UIFont systemFontOfSize:15];
        _thirdSectionHeaderView.textAlignment=1;
        
    }
    return _thirdSectionHeaderView;
}

-(UIView*)secondSectionfooterView
{
    if (!_secondSectionfooterView) {
        _secondSectionfooterView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, W_RATIO(100))];
        _secondSectionfooterView.backgroundColor=[UIColor whiteColor];
        NSArray*titleArray=@[@"近1月",@"近3月",@"近6月",@"近一年"];
        for (NSInteger i=0; i<titleArray.count; i++) {
            UIButton*fiveBtn=[UIButton buttonWithType:UIButtonTypeCustom];
            fiveBtn.frame=CGRectMake(screenWidth/4*i, 0,screenWidth/4, W_RATIO(100));
            [fiveBtn setTitle:titleArray[i] forState:UIControlStateNormal];
            fiveBtn.titleLabel.font=[UIFont systemFontOfSize:12];
            fiveBtn.backgroundColor=[UIColor whiteColor];
            if (i==0) {
                [fiveBtn setTitleColor:COLOR_NAV forState:UIControlStateNormal];
                _fiveBtnBottomLine=[[UIView alloc]initWithFrame:CGRectMake(0, fiveBtn.bottom-1, fiveBtn.width, 1)];
                _fiveBtnBottomLine.backgroundColor=COLOR_NAV;
                _lastFiveBtn=fiveBtn;
            }
            else
            {
                [fiveBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            }
            fiveBtn.tag=i+100;
            [fiveBtn addTarget:self action:@selector(fiveBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [_secondSectionfooterView addSubview:fiveBtn];
            
            [_secondSectionfooterView addSubview:_fiveBtnBottomLine];
        }
    }
    return _secondSectionfooterView;

}

-(void)twoBtnClick:(UIButton*)btn
{
    _lastBtn.backgroundColor=[UIColor whiteColor];
    [_lastBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.backgroundColor=COLOR_NAV;
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _lastBtn=btn;
}

-(void)fiveBtnClick:(UIButton*)btn
{
    [_lastFiveBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitleColor:COLOR_NAV forState:UIControlStateNormal];
    _lastFiveBtn=btn;
    if (btn.tag == 100) {
        if (self.oneMounth.count > 0) {
            [self setHtmlDataWith:_oneMounth];
        }else{
            [self getTrendDataWithDate:@"1"];
        }
        
    }else if (btn.tag == 101){
        if (self.threeMounth.count > 0) {
            [self setHtmlDataWith:_threeMounth];
        }else{
            [self getTrendDataWithDate:@"2"];
        }
    }else if (btn.tag == 102){
        if (self.sixMounth.count > 0) {
            [self setHtmlDataWith:_sixMounth];
        }else{
            [self getTrendDataWithDate:@"3"];
        }
    }else if (btn.tag == 103){
        if (self.oneYear.count > 0) {
            [self setHtmlDataWith:_oneYear];
        }else{
            [self getTrendDataWithDate:@"4"];
        }
    }
    [UIView animateWithDuration:0.2 animations:^{
       
        _fiveBtnBottomLine.frame=CGRectMake(btn.left, btn.bottom-1, btn.width, 1);
        
    }];
}

- (void)setHtmlDataWith:(NSMutableArray *)dateArray{
    NSMutableDictionary *htmlDict = [NSMutableDictionary dictionary];
    NSMutableArray *timeArray = [[NSMutableArray alloc] init];
    NSMutableArray *dataArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < dateArray.count; i++) {
        NSDictionary *dict = dateArray[i];
        [dataArray addObject:dict[@"performance"]];
        NSNumber *navDate = dict[@"navDate"];
        NSDateFormatter *stampFormatter = [[NSDateFormatter alloc] init];
        [stampFormatter setDateFormat:@"YYYY-MM-dd"];
        NSDate *stampDate2 = [NSDate dateWithTimeIntervalSince1970:[navDate doubleValue] / 1000];
        NSString *dateString = [stampFormatter stringFromDate:stampDate2];
        [timeArray addObject:dateString];
    }
    htmlDict[@"xData"] = timeArray;
    htmlDict[@"yData"] = [NSArray arrayWithObjects:dataArray, nil];
    htmlDict[@"legend"] = [NSArray arrayWithObjects:@"组合收益(%)", nil];
    NSString *str = [self dictionaryToJson:htmlDict];
    NSString *finalStr = [NSString stringWithFormat:@"initChart(%@)", str];
    [self.webView evaluateJavaScript:finalStr completionHandler:^(id _Nullable response, NSError * _Nullable error) {
        //TODO
    }];
}

//计算字符串高度
-(CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize string:(NSString *)string{
    
    NSDictionary *attrs = @{NSFontAttributeName:font};
    
    return [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
    
}

-(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    
}

-(void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
    
}

-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    NSMutableDictionary *htmlDict = [NSMutableDictionary dictionary];
    NSMutableArray *timeArray = [[NSMutableArray alloc] init];
    NSMutableArray *dataArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < _oneMounth.count; i++) {
        NSDictionary *dict = _oneMounth[i];
        [dataArray addObject:dict[@"performance"]];
        NSNumber *navDate = dict[@"navDate"];
        NSDateFormatter *stampFormatter = [[NSDateFormatter alloc] init];
        [stampFormatter setDateFormat:@"YYYY-MM-dd"];
        NSDate *stampDate2 = [NSDate dateWithTimeIntervalSince1970:[navDate doubleValue] / 1000];
        NSString *dateString = [stampFormatter stringFromDate:stampDate2];
        [timeArray addObject:dateString];
    }
    htmlDict[@"xData"] = timeArray;
    htmlDict[@"yData"] = [NSArray arrayWithObjects:dataArray, nil];
    htmlDict[@"legend"] = [NSArray arrayWithObjects:@"组合收益(%)", nil];
    NSString *str = [self dictionaryToJson:htmlDict];
    NSString *finalStr = [NSString stringWithFormat:@"initChart(%@)", str];
    [self.webView evaluateJavaScript:finalStr completionHandler:^(id _Nullable response, NSError * _Nullable error) {
        //TODO
        NSLog(@"%@__%@",error,error.localizedDescription);
    }];
    
}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 2) {
        
        NSArray *fundArray = self.makeUpData[@"portfolioFund"];
        NSDictionary *dict = fundArray[indexPath.row];
        HDCFFundDetailVc *vc = [[HDCFFundDetailVc alloc] init];
        vc.dataDict = dict;
        [self.navigationController pushViewController:vc animated:YES];
        
    }
}

@end
